<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */
include 'class/menupanel.php';
//Place code here
function list_menu(){
    global $db,$page,$pagesize;
    if (empty($page)){
        $start=0;
    } else {
         $start=$page * $pagesize; 
    }
    $sql="select a.*
    from _menu_panel a
    order by a.id asc limit $start,$pagesize";
    
    $result=$db->query($sql);
    echo "
<h2>Menu Panel</h2>     
<a href=\"?mod=panel&file=menus&action=add\">Add New Menu Panel</a>
<table width=\"100%\" cellspacing=0><tr><th>Icon</th>
        <th>URL</th><th>Menu Caption</th><th>Description</th><th>Action</th></tr>";
    while($row=$db->fetch_array($result)){
        extract($row);
        echo "<tr><td><img class=icon src=\"$menu_icon\"/></td>
        <td><a href=\"$menu_url\">Click to Show</a></td>";
        echo "<td><a href=\"?mod=panel&file=menus&action=edit&id=$id\">[$menu_caption]</a> </td>
        <td>$menu_desc</td>
        <td align=\"center\"><a href=\"?mod=panel&file=menus&action=delete&id=$id\">
        <img class=icon src=\"images/delete.png\" title=\"Edit Menu\"/></a>
        </td></tr>\r\n";
        
    }
    echo "</table>";
    
}
function add_menu(){
    echo "<h2>Add New Menu</h2>
        <form action=\"?mod=panel&file=menus\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
    
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td>Id</td><td></td></tr>
<tr><td>Menu Caption</td><td><input type=\"text\" name=\"menu_caption\" class=\"textbox\" value=\"\"/> </td></tr>
<tr><td>Menu Icon</td><td><input type=\"text\" name=\"menu_icon\" class=\"textbox\"
         value=\"\" size=40/> </td></tr>
<tr><td>Menu Url</td><td><input type=\"text\" name=\"menu_url\" class=\"textbox\" 
         value=\"\" size=60/> </td></tr>
<tr><td>Menu Desc</td><td><textarea rows=\"5\" cols=50 name=\"menu_desc\" class=\"textbox\" 
         ></textarea> </td></tr>
<tr><td>For Admin Only</td><td><input type=\"radio\" name=\"menu_admin\" 
         value=\"1\"/>Yes <input type=\"radio\" name=\"menu_admin\" 
         value=\"0\"/> No </td></tr>
<tr><td>Parent Id</td><td><input type=\"text\" name=\"parent_id\" class=\"textbox\"
         value=\"0\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function edit_menu(){
    global $db,$id;
    if (!empty($id)){
        $sql="select * from _menu_panel where id=$id";
        $row=$db->fetch_row($sql);
        if (!empty($row))extract($row);
        if ($menu_admin=='1'){
            $yesadmin="checked";
            $noadmin="";
        } else {
            $yesadmin="";
            $noadmin="checked";
        }
        echo "
        <h2>Edit Menu</h2>
        <form action=\"?mod=panel&file=menus\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
    <input type=\"hidden\" name=\"id\" value=\"$id\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td>Id</td><td>$id </td></tr>
<tr><td>Menu Caption</td><td><input type=\"text\" name=\"menu_caption\" class=\"textbox\" value=\"$menu_caption\"/> </td></tr>
<tr><td>Menu Icon</td><td><input type=\"text\" name=\"menu_icon\" class=\"textbox\"
         value=\"$menu_icon\" size=40/> </td></tr>
<tr><td>Menu Url</td><td><input type=\"text\" name=\"menu_url\" class=\"textbox\" 
         value=\"$menu_url\" size=60/> </td></tr>
<tr><td>Menu Desc</td><td><textarea rows=\"5\" cols=50 name=\"menu_desc\" class=\"textbox\" 
         >$menu_desc</textarea> </td></tr>
<tr><td>For Admin Only</td><td><input type=\"radio\" name=\"menu_admin\" 
         value=\"1\"/ $yesadmin>Yes <input type=\"radio\" name=\"menu_admin\" 
         value=\"0\"/ $noadmin> No</td></tr>
<tr><td>Parent Id</td><td><input type=\"text\" name=\"parent_id\" class=\"textbox\"
         value=\"$parent_id\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
    }
}
function edit_process(){
    global $db,$id,$menu_caption,$menu_icon,$menu_url,$menu_desc,$menu_admin,$parent_id;
    if (!empty($id)){
    $sql="update _menu_panel set menu_caption='$menu_caption',menu_icon='$menu_icon',menu_url='$menu_url',
    menu_desc='$menu_desc',menu_admin='$menu_admin',parent_id='$parent_id' where id=$id";
    $db->query($sql);
    refresh_to("?mod=panel&file=menus","Kembali ke menu panel",2);
    }
    
}
function add_process(){
    global $db,$id,$menu_caption,$menu_icon,$menu_url,$menu_desc,$menu_admin,$parent_id;
    if (!empty($menu_caption)){
    $sql="insert into _menu_panel values (null,'$menu_caption','$menu_icon','$menu_url','$menu_desc','$menu_admin','$parent_id')";
    $db->query($sql);
    refresh_to("?mod=panel&file=menus","Kembali ke menu panel",2);
    }
    
}
global $action;
switch ($action){
    case 'add':
        add_menu();
        break;
    case 'add_process':
        add_process();
        break;
    case 'edit':
        edit_menu();
        break;
    case 'edit_process':
        edit_process();
        break;
    default:
        list_menu();
        break;
}
?>